package com.github.niefy.modules.wx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.niefy.modules.wx.dao.AkNameMapper;
import com.github.niefy.modules.wx.entity.AkName;
import com.github.niefy.modules.wx.service.AkNameService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AkNameServiceImpl extends ServiceImpl<AkNameMapper, AkName> implements AkNameService {

    @Override
    public String queryName(String name) {

        LambdaQueryWrapper<AkName> queryWrapper = new LambdaQueryWrapper<AkName>();
        queryWrapper.eq(AkName::getDelFlag, "0").eq(AkName::getNickName, name).or().eq(AkName::getRealName, name);
        List<AkName> akNames = baseMapper.selectList(queryWrapper);

        StringBuffer sb = new StringBuffer();

        akNames.forEach(akName -> {
            sb.append(akName.getRealName()).append(" - ").append(akName.getNickName()).append("\n");
        });

        return sb.toString();
    }

    @Override
    public int checkNickName(String nickName) {
        LambdaQueryWrapper<AkName> queryWrapper = new LambdaQueryWrapper<AkName>();
        queryWrapper.eq(AkName::getDelFlag, "0").eq(AkName::getNickName, nickName);
        return baseMapper.selectCount(queryWrapper);
    }

    @Override
    public int checkRealName(String realName) {
        LambdaQueryWrapper<AkName> queryWrapper = new LambdaQueryWrapper<AkName>();
        queryWrapper.eq(AkName::getDelFlag, "0").eq(AkName::getRealName, realName);
        return baseMapper.selectCount(queryWrapper);
    }

    /**
     * 阿康花名规则
     * @param message
     * @return
     */
    public String akName(String message) {
        // @Q花名/真实姓名 查询     @R@真实姓名@花名 注册    @-D花名 删除
        if (message.startsWith("@Q")) {
            String[] text = message.split("@Q");
            if (text.length != 2) {
                return "您输入的指令有误，注册花名指令为：@Q花名";
            }
            String queryName = this.queryName(text[1].trim());
            if(!"".equals(queryName)) {
                return queryName;
            }
        } else if (message.startsWith("@R")) {
            String[] text = message.split("@");
            if (text.length != 4) {
                return "您输入的指令有误，注册花名指令为：@R@真实姓名@花名";
            }
            if (!text[3].startsWith("阿")) {
                return "阿康花名必须以阿开头";
            }
            if(this.checkNickName(text[3].trim()) > 0) {
                return "此花名已被注册，请换一个.";
            }
            if(this.checkRealName(text[2].trim()) > 0) {
                return "您已经注册过阿康花名了.";
            }
            AkName an = new AkName();
            an.setRealName(text[2].trim());
            an.setNickName(text[3].trim());
            if(this.save(an)) {
                return "注册成功！";
            } else {
                return "服务器开小差了，请联系开发人员处理.";
            }
        } else if (message.startsWith("@-D")) {
            if (!message.split("@-D")[1].trim().startsWith("阿")) {
                return "花名必须以阿开头";
            }
            if (this.update(new LambdaUpdateWrapper<AkName>().eq(AkName::getNickName, message.split("@-D")[1].trim()).set(AkName::getDelFlag,"1"))){
                return "删除成功！";
            }
        } else {
            StringBuffer sb = new StringBuffer();

            sb.append("查询花名：@q阿莱").append("\n");
            sb.append("注册花名：@r@邓海斌名@阿莱").append("\n");

            return sb.toString();
        }
        return "恭喜，此花名暂无人注册..";
    }
}
